package com.zhizuobiao.coura.stockhelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhizuobiao.coura.adapter.WeatherListAdapter;
import com.zhizuobiao.coura.application.MyApplication;
import com.zhizuobiao.coura.utils.DateUtil;
import com.zhizuobiao.coura.utils.WeatherUtil;
import com.zhizuobiao.coura.weather.Weather;
import com.zhizuobiao.coura.weather.WeatherService;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by coura on 2016/12/23.
 */

public class WeatherActivity extends AppCompatActivity {

    @BindView(R.id.iv_flag)
    ImageView ivFlag;
    @BindView(R.id.tv_todayWeather)
    TextView tvTodayWeather;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.iv_todayWeatherPic)
    ImageView ivTodayWeatherPic;
    @BindView(R.id.tv_todayWind)
    TextView tvTodayWind;
    @BindView(R.id.tv_updateTime)
    TextView tvUpdateTime;
    @BindView(R.id.weather_list)
    ListView weatherList;
    @BindView(R.id.iv_refresh)
    ImageView ivRefresh;
    @BindView(R.id.tv_realtimeTempreature)
    TextView tvRealtimeTempreature;
    @BindView(R.id.drawer_weather_img)
    ImageView drawerWeatherImg;
    @BindView(R.id.dl_left)
    DrawerLayout dlLeft;
    @BindView(R.id.iv_drawer_weather)
    ImageView ivDrawerWeather;
    @BindView(R.id.tv_drawer_weather)
    TextView tvDrawerWeather;
    @BindView(R.id.tv_drawer_temp)
    TextView tvDrawerTemp;
    @BindView(R.id.tv_drawer_chuanyi)
    TextView tvDrawerChuanyi;
    @BindView(R.id.tv_drawer_ziwaixian)
    TextView tvDrawerZiwaixian;
    @BindView(R.id.tv_drawer_ganmao)
    TextView tvDrawerGanmao;
    @BindView(R.id.tv_drawer_yundong)
    TextView tvDrawerYundong;
    @BindView(R.id.tv_drawer_addCity)
    TextView tvDrawerAddCity;
    @BindView(R.id.tv_drawer_deleteCity)
    TextView tvDrawerDeleteCity;
    @BindView(R.id.tv_drawer_xiche)
    TextView tvDrawerXiche;
    private MyApplication myApplication;
    private WeatherService weatherService;
    private WeatherListAdapter weatherListAdapter;
    private Weather weather;
    private List<Weather.ResultsBean.WeatherDataBean> weatherDataBeanList;
    private List<Weather.ResultsBean.IndexBean> indexBeanList;
    private String city;

    private static final String AK = "409eEic3j0WzAvoBNETnkiXwac6G87nu";
    private static final String OUTPUT = "json";
    private static final String MCODE = "9D:B5:4E:A6:A8:CF:E4:B6:B5:6C:48:41:96:22:89:C6:88:B3:BF:9A;com.zhizuobiao.coura.stockhelper";
    private static final String LOCATION = "徐州";  //默认城市
    private static final int REQUEST_CODE_PICK_CITY = 233;
    private static final int REQUEST_WEATHER_DETAIL = 222;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        myApplication = (MyApplication) getApplication();
        weatherService = myApplication.getWeatherService();
        loadWeatherDate(LOCATION);
        city = LOCATION;
    }

    private void loadWeatherDate(final String location) {
        Call<Weather> weatherCall = weatherService.getWeather(AK, OUTPUT, MCODE, location);
        weatherCall.enqueue(new Callback<Weather>() {
            @Override
            public void onResponse(Call<Weather> call, Response<Weather> response) {
                weather = response.body();
                if (weather.getError() == 0) {
                    weatherDataBeanList = weather.getResults().get(0).getWeather_data();
                    indexBeanList = weather.getResults().get(0).getIndex();
                    weatherListAdapter = new WeatherListAdapter(WeatherActivity.this, weatherDataBeanList);
                    weatherList.setAdapter(weatherListAdapter);
                    tvTodayWeather.setText(weatherDataBeanList.get(0).getWeather());
                    tvCity.setText(location);
                    tvTodayWind.setText(weatherDataBeanList.get(0).getWind());
                    tvUpdateTime.setText(DateUtil.getCurTime() + "发布");
                    tvRealtimeTempreature.setText(WeatherUtil.getRealTimeTemperature(weatherDataBeanList.get(0).getDate()));
                    ivTodayWeatherPic.setImageResource(WeatherUtil.getWeatherIcon(weatherDataBeanList.get(0).getWeather()));
                    drawerWeatherImg.setImageResource(WeatherUtil.getWeatherImg(weatherDataBeanList.get(0).getWeather()));
                    ivFlag.setImageResource(WeatherUtil.getFlag());
                    ivDrawerWeather.setImageResource(WeatherUtil.getWeatherIcon(weatherDataBeanList.get(0).getWeather()));
                    tvDrawerWeather.setText(weatherDataBeanList.get(0).getWeather());
                    tvDrawerTemp.setText(WeatherUtil.getRealTimeTemperature(weatherDataBeanList.get(0).getDate()));
                    tvDrawerChuanyi.setText(indexBeanList.get(0).getZs());
                    tvDrawerZiwaixian.setText(indexBeanList.get(5).getZs());
                    tvDrawerGanmao.setText(indexBeanList.get(3).getZs());
                    tvDrawerYundong.setText(indexBeanList.get(4).getZs());
                    tvDrawerXiche.setText(indexBeanList.get(1).getZs());
                }
            }

            @Override
            public void onFailure(Call<Weather> call, Throwable t) {
                Toast.makeText(WeatherActivity.this, "获取天气出错了，亲-_-!|", Toast.LENGTH_LONG).show();
            }
        });
    }

    @OnClick(R.id.iv_refresh)
    public void refresh() {
        loadWeatherDate(city);
    }


    @OnClick(R.id.tv_drawer_addCity)
    public void selectCity() {
        Intent intent = new Intent(WeatherActivity.this, CityPickerActivity.class);
        startActivityForResult(intent, REQUEST_CODE_PICK_CITY);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_CITY && resultCode == RESULT_OK) {
            if (data != null) {
                city = data.getStringExtra("city");
                loadWeatherDate(city);
                Toast.makeText(WeatherActivity.this, "选择的城市为：" + city, Toast.LENGTH_LONG).show();
            }
        }
        if (requestCode == REQUEST_WEATHER_DETAIL && resultCode == RESULT_OK){
            city = data.getStringExtra("city");
            loadWeatherDate(city);
        }
    }

    @OnClick(R.id.iv_drawer_weather)
    public void showWeatherDetail() {
        Intent intent = new Intent(WeatherActivity.this,WeatherDetailActivity.class);
        intent.putExtra("city",city);
        startActivityForResult(intent,REQUEST_WEATHER_DETAIL);
    }

}
